package com.davidlife.ExceptionHandle;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*
* 异常处理编写步骤：
    1. 编写自定义异常类(做提示信息的)： SysException
    2. 编写异常处理器： SysExceptionResolver
    3. 配置异常处理器(跳转到提示页面)：
        <!-- 配置异常处理器 -->
        <bean id="sysExceptionResolver" class="com.davidlife.ExceptionHandle.SysExceptionResolver"></bean>
 */
/**
 * 处理异常业务逻辑
 */
public class SysExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        //获取到异常对象
        SysException sysExp = null;
        if (e instanceof SysException) {
            sysExp = (SysException) e;
        } else {
            sysExp = new SysException("系统正在维护...");
        }

        //创建ModelAndView对象
        ModelAndView mv = new ModelAndView();
        mv.addObject("errorMsg", sysExp.getMessage());
        mv.setViewName("error");
        return mv;
    }
}
